package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.rpc.product.BaseCategory;
import com.atguigu.gmall.product.mapper.BaseCategory1Mapper;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BaseCategory1ServiceImpl extends ServiceImpl<BaseCategory1Mapper,BaseCategory1> implements BaseCategory1Service {

    @Autowired
    BaseCategory1Mapper baseCategory1Mapper;

    //线程安全的map
    Map<String,List<BaseCategory>> map = new HashMap<>();

    /**
     * 为首页准备三级分类数据
     * @return
     */
    @Override
    public List<BaseCategory> getAllCategoryForIndexPage() {

        List<BaseCategory> category = map.get("11");
        if (category == null){
            //缓存为空，查数据库,并且添加到缓存
            List<BaseCategory> list = baseCategory1Mapper.getAllCategoryForIndexPage();
            map.put("11",list);
            return category;
        }else {
            //缓存有东西中间返回
            return category;
        }
    }

    /**
     * 查询sku对应的分类所在完整路径
     * @param skuId
     * @return
     */
    @Override
    public BaseCategoryView getCategoryPathBySkuId(Long skuId) {
        return baseCategory1Mapper.getCategoryPathBySkuId(skuId);
    }
}
